package com.xqls.service.impl;

import com.xqls.bean.SamplingRecordBean;
import com.xqls.dal.dao.SamplingRecordDao;
import com.xqls.dal.model.SamplingRecord;
import com.xqls.dal.model.SamplingRecordExample;
import com.xqls.service.SamplingRecordService;
import com.xqls.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class SamplingRecordServiceImpl implements SamplingRecordService {

	@Autowired
	private SamplingRecordDao recordDao;

	@Override
	public boolean insertSamplingRecord(SamplingRecordBean recordBean) {
        //采样仪记录 设备编号 采样时间 只保存一条记录
		SamplingRecord record = (SamplingRecord) ObjectUtil.transfer(recordBean, SamplingRecord.class);
        SamplingRecordExample example =new SamplingRecordExample();
        example.createCriteria().andDeviceNoEqualTo(record.getDeviceNo()).andRecordingTimeEqualTo(record.getRecordingTime());
        SamplingRecord recordData=  recordDao.queryByExample(example);
		if(recordData==null){
			return recordDao.insertSamplingRecordList(record) > 0;
		}
		else{
			return true;
		}
	}

	public boolean updateSamplingRecord(SamplingRecordBean recordBean) {
		SamplingRecord record = (SamplingRecord) ObjectUtil.transfer(recordBean, SamplingRecord.class);
		return recordDao.updateSamplingRecordList(record)>0;
	}
	
	public SamplingRecordBean queryByRecordNo(Integer recordNo){
		return (SamplingRecordBean) ObjectUtil.transfer(recordDao.queryByRecordNo(recordNo), SamplingRecordBean.class);
	}

	@Override
	public List<Map<String, Object>> selectByMap(Map<String, Object> map, String sampleType) {

		return recordDao.selectByMap(map, sampleType);
	}
	
	public SamplingRecord queryDischargeByDeviceNo(String deviceNo){
		return recordDao.queryDischargeByDeviceNo(deviceNo);
	}
	
	public SamplingRecord queryRecordByDeviceNo(String deviceNo,String sampleType){
		return recordDao.queryRecordByDeviceNo(deviceNo, sampleType);
	}

	@Override
	public List<Map<String, Object>> getListByMap(Map<String, Object> map) {
		return this.recordDao.getListByMap(map);
	}
	
	
	public SamplingRecordBean transfer(SamplingRecord record){
		SamplingRecordBean recordBean=new SamplingRecordBean();
		recordBean.setBottleNo(record.getBottleNo());
		recordBean.setCod(record.getCod());
		recordBean.setDeviceNo(record.getDeviceNo());
		recordBean.setNh3(record.getNh3());
		recordBean.setOperationSource(record.getOperationSource());
		recordBean.setPh(record.getPh());
		recordBean.setRecordingTime(record.getRecordingTime());
		recordBean.setRecordNo(record.getRecordNo());
		recordBean.setSampleRetention(record.getSampleRetention());
		recordBean.setSampleType(record.getSampleType());
		return recordBean;
	}
	
	public SamplingRecord transfer(SamplingRecordBean recordBean){
		SamplingRecord record=new SamplingRecord();
		record.setBottleNo(recordBean.getBottleNo());
		record.setCod(recordBean.getCod());
		record.setDeviceNo(recordBean.getDeviceNo());
		record.setNh3(recordBean.getNh3());
		record.setOperationSource(recordBean.getOperationSource());
		record.setPh(recordBean.getPh());
		record.setRecordingTime(recordBean.getRecordingTime());
		record.setRecordNo(recordBean.getRecordNo());
		record.setSampleRetention(recordBean.getSampleRetention());
		record.setSampleType(recordBean.getSampleType());
		return record;
	}

}
